<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Role extends Model
{
	use SoftDeletes;

    protected $table = 'role';

    protected $fillable = ['role_name'];

	/**
	 * 多对多关联角色权限表
	 * User: ihome
	 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
	 */
	public function role_permission()
    {
		return $this->belongsToMany(Permission::class,'role_permission','role_id','permission_id')->withTimestamps();
    }

	/**
	 * 多对多关联管理员角色表
	 * User: Clannad ~ ☆
	 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
	 */
	public function role_admin()
    {
		return $this->belongsToMany(Admin::class,'admin_role','role_id','admin_id');
    }

	/**
	 * 更新权限缓存(废弃)
	 * User: ihome
	 * @param $role_id
	 * @throws \Exception
	 */
	public function updatePermissionCache($roleIds = [])
    {
		//验证是否有权访问某个链接
		if($roleIds){

			//普通管理员
			$permissionList = [];

			//所有角色的权限id
			$allRolePermissionIds = [];

		    foreach ($roleIds as $k => $role_id) {
				$role = Role::findOrFail($role_id);

				$permissionIds = $role->role_permission()->select('permission_id')->get()->toArray();
				$permissionIds = array_column($permissionIds,'permission_id');

				$allRolePermissionIds = array_filter(array_merge($permissionIds,$allRolePermissionIds));
				//查询所有权限列表
				$permission = Permission::query()
					->whereIn('id',$permissionIds)
					->select('route_url')
					->get()->toArray();

				$permission = array_filter(array_column($permission,'route_url'));

				$permissionList = array_merge($permissionList,$permission);
		    }

		    array_unique($permissionList);
		    
			cache(['admin:'.auth('admin')->id().':permissionList'=>json_encode($permissionList)],86400);

			//获得菜单
			$this->getRoleMenu(array_unique($allRolePermissionIds));
		}
    }

	/**
	 * 获得当前拥有角色的所有权限id
	 * User: Clannad ~ ☆
	 * DataTime：2019/11/18 0018 22:52
	 * @param $roleIds
	 * @return array
	 */
	public function getRolePermissionIds($roleIds)
    {
		//所有角色的权限id
		$allRolePermissionIds = [];

		foreach (explode(',',$roleIds['role_ids']) as $k => $role_id) {
			$role = Role::find($role_id);

			if($role){
				$permissionIds = $role->role_permission()->select('permission_id')->get()->toArray();
				$permissionIds = array_column($permissionIds,'permission_id');

				//所有的权限id去重后的数据
				$allRolePermissionIds = array_filter(array_merge($permissionIds,$allRolePermissionIds));
			}
		}

		return $allRolePermissionIds;
    }



	/**
	 * 菜单
	 * User: ihome
	 * @param array $permissionsIds
	 * @return array
	 * @throws \Exception
	 */
	public function getMenu($admin,$permissionsIds = [])
    {
			//超级管理员
			if($admin == 'super'){
				$permissionList = Permission::query()
					->select('route_url','id','per_icon','permission_name')
					->where('pid','=','0')->get()->toArray();

				foreach ($permissionList as $k => $v) {
					$permissionList[$k]['child'] = Permission::query()
						->select('route_url','permission_name')
						->where('pid','=',$v['id'])
						->get()
						->toArray();
				}

				return $permissionList;

			}else if($admin == 'admin'){
				$permissionList = [];

				if($permissionsIds){
					$permissions = Permission::query()
						->select('route_url','pid','id','per_icon','permission_name')
						->whereIn('id',$permissionsIds)->get()->toArray();

					foreach ($permissions as $k => $v) {
						if($v['pid'] == 0){
							foreach ($permissions as $k1 => $v1) {
								if($v1['pid'] == $v['id']){
									$v['child'][] = $v1;
								}
							}
							$permissionList[] = $v;
						}
					}

					return $permissionList;
				}

			}
    }

}
